using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Dracham.TileManagement.CharacterManagement;
using Dracham.TileManagement.ZoneManagement.Actions;

namespace Dracham.TileManagement
{
    public class ZoneTile
    {
        private DrachamTexture backgroundTexture;
        private DrachamTexture decorationTexture;
        private Int32 backgroundTextureX = 0;
        private Int32 backgroundTextureY = 0;
        private Int32 decorationTextureX = 0;
        private Int32 decorationTextureY = 0;
        private Int32 destX = 0;
        private Int32 destY = 0;
        private Boolean solid = false;

        private Zone zone;

        private List<global::Dracham.TileManagement.ZoneManagement.Actions.Action> onEnterActions = new List<global::Dracham.TileManagement.ZoneManagement.Actions.Action>();

        internal ZoneTile(XmlNode objectNode, Zone zone)
        {
            if (objectNode.Attributes["BackgroundTexture"] != null)
            {
                backgroundTexture = TextureManager.GetTexture(objectNode.Attributes["BackgroundTexture"].Value);
                backgroundTextureX = Convert.ToInt32(objectNode.Attributes["BackgroundTextureX"].Value);
                backgroundTextureY = Convert.ToInt32(objectNode.Attributes["BackgroundTextureY"].Value);
            }

            if (objectNode.Attributes["DecorationTexture"] != null)
            {
                decorationTexture = TextureManager.GetTexture(objectNode.Attributes["DecorationTexture"].Value);
                decorationTextureX = Convert.ToInt32(objectNode.Attributes["DecorationTextureX"].Value);
                decorationTextureY = Convert.ToInt32(objectNode.Attributes["DecorationTextureY"].Value);
            }

            destX = Convert.ToInt32(objectNode.Attributes["X"].Value);
            destY = Convert.ToInt32(objectNode.Attributes["Y"].Value);

            if (objectNode.Attributes["Solid"] != null)
            {
                solid = Convert.ToBoolean(objectNode.Attributes["Solid"].Value);
            }
            else
            {
                solid = false;
            }

            //
            // parse events
            foreach (XmlNode e in objectNode.ChildNodes)
            {
                if (e.Name == "OnEnter")
                {
                    ParseActions(e, ref onEnterActions);
                }
            }

            this.zone = zone;
        }

        private void ParseActions(XmlNode node, ref List<global::Dracham.TileManagement.ZoneManagement.Actions.Action> actionList)
        {
            foreach (XmlNode a in node.ChildNodes)
            {
                if (a.Name == "Teleport")
                {
                    actionList.Add(new Teleport(a));
                }
            }
        }

        #region properties

        public Int32 BackgroundTextureX { get { return backgroundTextureX; } }
        public Int32 BackgroundTextureY { get { return backgroundTextureY; } }

        public Int32 DecorationTextureX { get { return decorationTextureX; } }
        public Int32 DecorationTextureY { get { return decorationTextureY; } }

        public Int32 DestX { get { return destX; } }
        public Int32 DestY { get { return destY; } }
        public Boolean Solid { get { return solid; } }

        #endregion

        public void UnInitialize()
        {
            //
            // TODO: UnInitialize
        }

        public void Render(SpriteBatch batch, Int32 posx, Int32 posy, Int32 tx, Int32 ty, Player player)
        {
            Int32 tilePosX = (6 + (destX - posx));
            Int32 tilePosY = (6 + (destY - posy));

            if (destX == posx && destY == posy)
            {
                player.CurrentZoneObject = this;
            }

            if (backgroundTexture != null)
            {
                batch.Draw(
                    backgroundTexture.Texture,
                    new Microsoft.Xna.Framework.Rectangle(
                        (tilePosX * Dracham.TileWidth) + tx + (Dracham.TileWidth * 8), // X
                        (tilePosY * Dracham.TileHeight) + ty, // Y
                        Dracham.TileWidth,
                        Dracham.TileHeight),
                    new Microsoft.Xna.Framework.Rectangle(
                        backgroundTextureX * Dracham.TileWidth, // X
                        backgroundTextureY * Dracham.TileHeight, // Y
                        Dracham.TileWidth,
                        Dracham.TileHeight),
                    Color.White);
            }

            if (decorationTexture != null)
            {
                batch.Draw(
                    decorationTexture.Texture,
                    new Microsoft.Xna.Framework.Rectangle(
                        (tilePosX * Dracham.TileWidth) + tx + (Dracham.TileWidth * 8), // X
                        (tilePosY * Dracham.TileHeight) + ty, // Y
                        Dracham.TileWidth,
                        Dracham.TileHeight),
                    new Microsoft.Xna.Framework.Rectangle(
                        decorationTextureX * Dracham.TileWidth, // X
                        decorationTextureY * Dracham.TileHeight, // Y
                        Dracham.TileWidth,
                        Dracham.TileHeight),
                    Color.White);
            }

        }

        public void OnEnter()
        {
            this.onEnterActions.ForEach(delegate(global::Dracham.TileManagement.ZoneManagement.Actions.Action a)
            {
                a.RunAction();
            });
        }

        public void OnLeave()
        {
            //throw new Exception("The method or operation is not implemented.");
        }
    }
}
